<?php defined('SYSPATH') or die('No direct script access.');

class Task_Sendmail extends Minion_Task {
	/**
	 * Test action
	 *
	 * @param array $params
	 * @return void
	 */
	protected function _execute(array $params)
	{
		Minion_CLI::write(Minion_CLI::color('sendemail started at '.date('H:i:s'),'green'));
		$stack = Model::factory('Book')->from_stack();
		if (empty($stack))
		{
			/*
			// Sample progress indicator
			Minion_CLI::write_replace(Minion_CLI::color('0%','red'));
			sleep(1);
			Minion_CLI::write_replace(Minion_CLI::color('25%','red'));
			sleep(1);
			Minion_CLI::write_replace(Minion_CLI::color('50%','red'));
			sleep(1);
			Minion_CLI::write_replace(Minion_CLI::color('75%','red'));
			sleep(1);
			// Done writing this line
			Minion_CLI::write_replace(Minion_CLI::color('100%','green'), TRUE);
			sleep(1);
			*/
			Minion_CLI::write(Minion_CLI::color('stack is empty!','green'));
		}
		else
		{
			foreach ($stack as $row)
			{
				$message = unserialize($row['data']);
				if(Email::send('default', $message['subject'], $message['body'], $message['from'], $message['to'], 'text/html')==TRUE)
				{
					Minion_CLI::write(Minion_CLI::color('message sended to "'.key($message['to']).'" with subject "'.$message['subject'].'"','green'));
					Model::factory('Book')->delete('emailstack',array('id'=>$row['id']));
				}
			}
		}
		Minion_CLI::write(Minion_CLI::color('sendemail ended at '.date('H:i:s'),'green'));
	}

} // End Sendmail
